package com.yjt.common.exceptions;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * @author 曾智平
 * @ClassName: CustomAccessDeniedHandler
 * @Description: 重写权限不足问题
 * @date 2021/9/27 10:44
 */
@Component
public class CustomAccessDeniedHandler implements AccessDeniedHandler {
    @Override

    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException {
        JSONObject resultObject = new JSONObject();
        resultObject.put("code",HttpServletResponse.SC_UNAUTHORIZED);
        resultObject.put("message","权限不足");
        response.setHeader("Content-Type", "application/json;charset=UTF-8");
        response.setStatus(HttpServletResponse.SC_OK);
        response.getWriter().write(JSON.toJSONString(resultObject));
    }

}
